<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-点击修改元素背景颜色</title>
    <style>
        .container{
            width: 400px;
            height: 300px;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        function rand(m,n){
            return Math.ceil(Math.random() * (n-m+1))+m-1;
        }


        class App extends React.Component {
            state = {
                bg: 'rgb(200, 200, 200)',
            }

            render(){
                return <div 
                        className="container" 
                        onClick={this.changeBg} 
                        style={{background: this.state.bg}}>
                    
                    </div>
            }

            changeBg = () => {
                //修改div的背景颜色
                // 数据驱动视图
                this.setState({
                    bg: `rgb(${rand(0, 255)}, ${rand(0, 255)}, ${rand(0, 255)})`
                })
            }
        }

        //渲染
        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>